package gui.data.D2;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.QuadrangleCalculator;

@SuppressWarnings("serial")
public class QuadrangleInputGui extends JPanel implements ItemListener {
	int guide = 5;
	JTextField adInput, dcInput, cbInput, baInput, dabAngleInput, abcAngleInput,
			bcdAngleInput, cdaAngleInput, d1Input, d2Input, areaInput,
			circumfInput, heightInput;
	MyFrame myFrame;
	JLabel adLbl, dcLbl, cbLbl, baLbl, dabAngle, abcAngle, bcdAngle, cdaAngle,
			d1Lbl, d2Lbl, areaLbl, circumfLbl, heightLbl;

	public QuadrangleInputGui(final MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(0, 4));
		this.setPreferredSize(new Dimension(500, 500));

		String[] shapes = { "Square", "Oblong", "Rhombus", "Rhomboid",
				"Trapezium", "Kite" };
		JComboBox shapeSelector = new JComboBox(shapes);
		shapeSelector.setSelectedIndex(4);
		shapeSelector.addItemListener(this);

		adLbl = new JLabel("Length of |AD|");
		dcLbl = new JLabel("Length of |DC|");
		cbLbl = new JLabel("Length of |CB|");
		baLbl = new JLabel("Length of |BA|");

		dabAngle = new JLabel("Angle " + "\u2220DAB");
		abcAngle = new JLabel("Angle " + "\u2220ABC");
		bcdAngle = new JLabel("Angle " + "\u2220BCD");
		cdaAngle = new JLabel("Angle " + "\u2220CDA");

		adInput = new JTextField();
		dcInput = new JTextField();
		cbInput = new JTextField();
		baInput = new JTextField();

		dabAngleInput = new JTextField();
		abcAngleInput = new JTextField();
		bcdAngleInput = new JTextField();
		cdaAngleInput = new JTextField();

		d1Lbl = new JLabel("Diagonal 1 (|AC|)");
		d2Lbl = new JLabel("Diagonal 2 (|DB|)");

		d1Input = new JTextField();
		d2Input = new JTextField();

		circumfLbl = new JLabel("Circumference");
		areaLbl = new JLabel("Area");

		circumfInput = new JTextField();
		areaInput = new JTextField();

		heightLbl = new JLabel("Height (AB to DC)");
		heightInput = new JTextField();

		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel("Type of quadrangle"));
		this.add(shapeSelector);
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		this.add(new JLabel(""));
		// side lengths
		this.add(adLbl);
		this.add(adInput);
		this.add(dcLbl);
		this.add(dcInput);
		this.add(cbLbl);
		this.add(cbInput);
		this.add(baLbl);
		this.add(baInput);
		// angles
		this.add(dabAngle);
		this.add(dabAngleInput);
		this.add(abcAngle);
		this.add(abcAngleInput);
		this.add(bcdAngle);
		this.add(bcdAngleInput);
		this.add(cdaAngle);
		this.add(cdaAngleInput);
		// diagonals
		this.add(d1Lbl);
		this.add(d1Input);
		this.add(d2Lbl);
		this.add(d2Input);
		// circumference and area
		this.add(circumfLbl);
		this.add(circumfInput);
		this.add(areaLbl);
		this.add(areaInput);
		// height
		this.add(heightLbl);
		this.add(heightInput);
		// calculate
		this.add(new JLabel(""));
		this.add(calculate);
	}

	@SuppressWarnings("unused")
	private void openResult() {
		boolean error = false;
		JTextField[] textFields = {adInput, dcInput, cbInput, baInput, dabAngleInput, abcAngleInput,
				bcdAngleInput, cdaAngleInput, d1Input, d2Input, circumfInput,
				areaInput, heightInput};
		Double[] values = new Double[13];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new QuadrangleCalculator(guide, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10], values[11], values[12],  myFrame);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getItem().equals("Square")) {
			guide = 1;
			// only one side needed
			adInput.setEnabled(true);
			dcInput.setEnabled(false);
			cbInput.setEnabled(false);
			baInput.setEnabled(false);

			// no angles needed
			dabAngleInput.setEnabled(false);
			abcAngleInput.setEnabled(false);
			bcdAngleInput.setEnabled(false);
			cdaAngleInput.setEnabled(false);

			// second diagonal and height not needed
			d1Input.setEnabled(true);
			d2Input.setEnabled(false);
			heightInput.setEnabled(false);

			// area and circumference needed
			areaInput.setEnabled(true);
			circumfInput.setEnabled(true);

		} else if (e.getItem().equals("Oblong")) {
			guide = 2;
			// two sides needed
			adInput.setEnabled(true);
			dcInput.setEnabled(true);
			cbInput.setEnabled(false);
			baInput.setEnabled(false);

			// no angles needed
			dabAngleInput.setEnabled(false);
			abcAngleInput.setEnabled(false);
			bcdAngleInput.setEnabled(false);
			cdaAngleInput.setEnabled(false);

			// second diagonal and height not needed
			d1Input.setEnabled(true);
			d2Input.setEnabled(false);
			heightInput.setEnabled(false);

			// area and circumference needed
			areaInput.setEnabled(true);
			circumfInput.setEnabled(true);
		} else if (e.getItem().equals("Rhombus")) {
			guide = 3;
			// only one side needed
			adInput.setEnabled(true);
			dcInput.setEnabled(false);
			cbInput.setEnabled(false);
			baInput.setEnabled(false);

			// two different angles needed
			dabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(true);
			bcdAngleInput.setEnabled(false);
			cdaAngleInput.setEnabled(false);

			// diagonals and height needed
			d1Input.setEnabled(true);
			d2Input.setEnabled(true);
			heightInput.setEnabled(true);

			// area and circumference needed
			areaInput.setEnabled(true);
			circumfInput.setEnabled(true);
		} else if (e.getItem().equals("Rhomboid")) {
			guide = 4;
			// two sides needed
			adInput.setEnabled(true);
			dcInput.setEnabled(false);
			cbInput.setEnabled(true);
			baInput.setEnabled(false);

			// two different angles needed
			dabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(true);
			bcdAngleInput.setEnabled(false);
			cdaAngleInput.setEnabled(false);

			// diagonals and height needed
			d1Input.setEnabled(true);
			d2Input.setEnabled(true);
			heightInput.setEnabled(true);

			// area and circumference needed
			areaInput.setEnabled(true);
			circumfInput.setEnabled(true);
		} else if (e.getItem().equals("Trapezium")) {
			guide = 5;
			// all sides needed
			adInput.setEnabled(true);
			dcInput.setEnabled(true);
			cbInput.setEnabled(true);
			baInput.setEnabled(true);

			// all angles needed
			dabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(true);
			bcdAngleInput.setEnabled(true);
			cdaAngleInput.setEnabled(true);

			// diagonals and height needed
			d1Input.setEnabled(true);
			d2Input.setEnabled(true);
			heightInput.setEnabled(true);

			// area and circumference needed
			areaInput.setEnabled(true);
			circumfInput.setEnabled(true);
		} else if (e.getItem().equals("Kite")) {
			guide = 6;
			// two sides needed
			adInput.setEnabled(true);
			dcInput.setEnabled(true);
			cbInput.setEnabled(false);
			baInput.setEnabled(false);

			// two different angles needed
			dabAngleInput.setEnabled(true);
			abcAngleInput.setEnabled(true);
			bcdAngleInput.setEnabled(true);
			cdaAngleInput.setEnabled(false);

			// diagonals needed and height not needed
			d1Input.setEnabled(true);
			d2Input.setEnabled(true);
			heightInput.setEnabled(false);

			// area and circumference needed
			areaInput.setEnabled(true);
			circumfInput.setEnabled(true);
		}

	}
}
